package ve.usb.ddp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import ve.usb.cohesion.runtime.CohesionAction;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import ve.usb.cohesion.runtime.HibernateUtil;
import ve.usb.ddp.hibernate.*;


/**
 * Manejo de elementos de la entidad Paa.
 */
public class AccionesPaa extends CohesionAction {
    
/**
     * Called by Struts for the execution of action APreCrear_paa_profesor.
     * Prepara la vista de captura de datos de una entidad para realizar una inserción.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward APreCrear_paa_profesor(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"VCrear_paa_profesor", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FPaa fFPaa = FPaa.clear(mapping, request);
             
            //Feed for combo listProfesor.
            {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(ci, idProfesor) " + 
               "from Profesor").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFPaa.setListProfesor(list);
            }

            fFPaa.setProfesor(Long.parseLong((String)request.getParameter("idProfesor_super")));

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action APre_paa.
     * Prepara la vista de actualización de una entidad.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward APre_paa(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"V_paa", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            Long idPaa = lookup_Long(request, "idPaa");
            
            FPaa fFPaa = FPaa.clear(mapping, request);
            Paa paa = (Paa)s.load(Paa.class, idPaa);
            fFPaa.setIdPaa(paa.getIdPaa());

            fFPaa.setProfesor(paa.getProfesor().getIdProfesor());

            fFPaa.setUniversidad(paa.getUniversidad());
            fFPaa.setPais(paa.getPais());
            fFPaa.setModalidad(paa.getModalidad());
            fFPaa.setFecha_inicio(paa.getFecha_inicio().toString());
            fFPaa.setFecha_culminacion(paa.getFecha_culminacion().toString());
            fFPaa.setFuente_financiamiento(paa.getFuente_financiamiento());
            fFPaa.setTipo_financiamiento(paa.getTipo_financiamiento());
            fFPaa.setMonto_financiamiento(paa.getMonto_financiamiento());

            //Feed for combo listProfesor.
            {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(ci, idProfesor) " + 
               "from Profesor").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFPaa.setListProfesor(list);
            }

            for (Observacion_Paa _Observacion_Paa : paa.getObservaciones()) {
                Hibernate.initialize(_Observacion_Paa.getPaa());
            }
            request.setAttribute("L_Observacion_Paa", paa.getObservaciones());


            request.getSession().setAttribute("idProfesor_super", fFPaa.getProfesor());

            request.getSession().setAttribute("idPaa", idPaa);
            request.getSession().setAttribute("idPaa_super", idPaa);

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action AAct_paa.
     * Registro de actualizaciones de Paa.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward AAct_paa(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"V_paa", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FPaa fFPaa = (FPaa)form;
            Paa paa = (Paa)s.load(Paa.class, fFPaa.getIdPaa());
            paa.setUniversidad(fFPaa.getUniversidad());
            paa.setPais(fFPaa.getPais());
            paa.setModalidad(fFPaa.getModalidad());
            paa.setFecha_inicio(new ve.usb.cohesion.runtime.LocalDate(fFPaa.getFecha_inicio()));
            paa.setFecha_culminacion(new ve.usb.cohesion.runtime.LocalDate(fFPaa.getFecha_culminacion()));
            paa.setFuente_financiamiento(fFPaa.getFuente_financiamiento());
            paa.setTipo_financiamiento(fFPaa.getTipo_financiamiento());
            paa.setMonto_financiamiento(fFPaa.getMonto_financiamiento());


            //Feed for combo listProfesor.
            {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(ci, idProfesor) " + 
               "from Profesor").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFPaa.setListProfesor(list);
            }

            for (Observacion_Paa _Observacion_Paa : paa.getObservaciones()) {
                Hibernate.initialize(_Observacion_Paa.getPaa());
            }
            request.setAttribute("L_Observacion_Paa", paa.getObservaciones());


            request.getSession().setAttribute("idProfesor_super", fFPaa.getProfesor());

            request.getSession().setAttribute("idPaa", paa.getIdPaa());

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        if (salida==0) {
          request.setAttribute("msg",
            getResources(request).getMessage("AAct_paa.msg0"));
        }

        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action AElim_paa.
     * Elimina el elemento seleccionado de Paa.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward AElim_paa(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"APre_profesor", "V_paa", };
        final int SALIDA_0 = 0;
        final int SALIDA_1 = 1;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            Long idPaa = lookup_Long(request, "idPaa");
            Paa paa = (Paa)s.load(Paa.class, idPaa);

            long idProfesor_super = paa.getProfesor().getIdProfesor();
            paa.getProfesor().removeFromPaa(paa);

            try {
                s.flush();
                request.getSession().setAttribute("idPaa", idPaa);

                request.getSession().setAttribute("idProfesor_super", idProfesor_super);

            } catch (Exception ex1) {
                ex1.printStackTrace();
                salida = SALIDA_1;
            }

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        if (salida==0) {
          request.setAttribute("msg",
            getResources(request).getMessage("AElim_paa.msg0"));
        }
        if (salida==1) {
          request.setAttribute("msg",
            getResources(request).getMessage("AElim_paa.msg1"));
        }

        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action ACrear_paa.
     * Registro de la creación de un elemento de Paa.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward ACrear_paa(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"V_paa", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FPaa fFPaa = (FPaa)form;
            Paa paa = new Paa();
            paa.setUniversidad(fFPaa.getUniversidad());
            paa.setPais(fFPaa.getPais());
            paa.setModalidad(fFPaa.getModalidad());
            paa.setFecha_inicio(new ve.usb.cohesion.runtime.LocalDate(fFPaa.getFecha_inicio()));
            paa.setFecha_culminacion(new ve.usb.cohesion.runtime.LocalDate(fFPaa.getFecha_culminacion()));
            paa.setFuente_financiamiento(fFPaa.getFuente_financiamiento());
            paa.setTipo_financiamiento(fFPaa.getTipo_financiamiento());
            paa.setMonto_financiamiento(fFPaa.getMonto_financiamiento());


            {Profesor obj = (Profesor)s.load(Profesor.class, fFPaa.getProfesor());
             obj.addToPaa(paa);
            }

            s.flush();
            fFPaa.setIdPaa(paa.getIdPaa());
            //Feed for combo listProfesor.
            {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(ci, idProfesor) " + 
               "from Profesor").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFPaa.setListProfesor(list);
            }


            //fFPaa.setProfesor(paa.getProfesor().getIdProfesor());
            request.getSession().setAttribute("idProfesor_super", fFPaa.getProfesor());

            request.getSession().setAttribute("idPaa", paa.getIdPaa());
            request.getSession().setAttribute("idPaa_super", paa.getIdPaa());

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        if (salida==0) {
          request.setAttribute("msg",
            getResources(request).getMessage("ACrear_paa.msg0"));
        }

        return mapping.findForward(SALIDAS[salida]);
    }



}